package com.baosight.sczx.zm.cc.service;
import java.util.HashMap;
import java.util.Map;

import com.baosight.iplat4j.core.ei.EiBlock;
import com.baosight.iplat4j.core.ei.EiConstant;
import com.baosight.iplat4j.core.ei.EiInfo;
import com.baosight.iplat4j.core.threadlocal.UserSession;
import com.baosight.iplat4j.ep.ServiceEPBase;
import com.baosight.sczx.common.StaticBlocks;
import com.baosight.sczx.common.zm.basetools.StaticTools;
import com.baosight.sczx.common.zm.basetools.TablePk;
import com.baosight.sczx.common.zm.cInterface.Department;
import com.baosight.sczx.common.zm.cInterface.cs.InitialData;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblem;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemHandle;
import com.baosight.sczx.zm.cc.domain.ZMCCProduceproblemPenalty; 

/**
 * 
 * <p>Title: ServiceZMCC11.java</p>
 * <p>Description: 生产问题处理页面</p>
 * <p>Copyright: Baosight Software LTD.co Copyright (c) 2002</p>
 * <p>Company: Baosight Software</p>
 * @author 郭淑芳
 * @version 1.0
 * <pre>Histroy:
 *          2016年9月28日 上午10:51:43  郭淑芳  Create
 *</pre>
 */
public class ServiceZMCC11 extends ServiceEPBase {

	/**
	 * 
	 * <p>Function: initLoad</p>
	 * <p>Description: 初始化进入方法</p>
	 * <p>return value: ServiceZMCC11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:51:55 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo initLoad(EiInfo inInfo) {
		inInfo.addBlock(EiConstant.resultBlock).set("showCount", "true");// 如果不加这个，翻页的统计和显示是错误的
		EiInfo outInfo = this.query(inInfo);
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: query</p>
	 * <p>Description: 查询方法</p>
	 * <p>return value: ServiceZMCC11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:52:04 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo query(EiInfo inInfo) {
		String miningCode = UserSession.getInSessionProperty("miningCode").toString();
		StaticTools.setQueryParameters(inInfo, "fdMiningId", miningCode, true);
		StaticTools.setQueryParameters(inInfo, "isHandling", "yes", true);// 所有未作废\关闭的隐患

		EiInfo outInfo = super.query(inInfo, "ZMCCProduceproblem.query", new ZMCCProduceproblem());

		outInfo.addBlock(Department.getInstance().getMiningDepts("cmes.cp.department", "orgCode", "orgName", true, UserSession.getInSessionProperty("miningCode").toString()));// 责任部门
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemType", true));// 问题类型
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemLevel", true));// 问题级别
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cp.problemOrigin", true));// 问题来源
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.problemStatus", new String[]{"0", "1", "2", "3", "4", "5", "6"}, new String[]{"草拟", "未处理", "未查收", "未整改", "未核查", "关闭", "作废"}, true));//问题状态
		outInfo.addBlock(StaticBlocks.getBlockData("cmes.cp.isCheck", new String[] { "否", "是" }, new String[] { "否", "是" }, true));// 是否重复问题
		outInfo.addBlock(InitialData.getInstance().getEiBlockByNo("cmes.cl.penaltyType"));

		outInfo.set("fdCreator", UserSession.getInSessionProperty("UserCname"));
		outInfo.set("fdCreateDepartmentId", UserSession.getInSessionProperty("orgCode"));// 单位的默认值
		outInfo.set("fdMiningId", UserSession.getInSessionProperty("miningCode"));// 矿ID的默认值
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: insert</p>
	 * <p>Description: 新增方法</p>
	 * <p>return value: ServiceZMCC11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:47:07 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo insert(EiInfo inInfo) {
		String fdMiningId = (String) UserSession.getInSessionProperty("miningCode");// 矿ID的默认值
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();

		for (int i = 0; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String problemTimeF = _dom.getFdProblemTimeF();
			String deadlineTimeF = _dom.getFdDeadlineTimeF();

			_dom.setFdProblemDate(this.getDay(problemTimeF)); // 发生日期
			_dom.setFdProblemTime(this.getTime(problemTimeF)); // 发生时间
			_dom.setFdDeadlineDate(this.getDay(deadlineTimeF)); // 解决日期
			_dom.setFdDeadlineTime(this.getTime(deadlineTimeF)); // 解决时间
			_dom.setFdCreateTime(StaticTools.getDateTime());
			_dom.setFdMiningId(fdMiningId);// 矿井id
			_dom.setFdCreateUserId(UserSession.getUserId().toString());
			_dom.setFdCreateUser(UserSession.getInSessionProperty("UserCname") == null ? "" : UserSession.getInSessionProperty("UserCname").toString());
			_dom.setFdSubmitorDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdSubmitorDepartmentId()));
			_dom.setFdBusinessDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdBusinessDepartmentId()));
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM"));

			this.dao.insert("ZMCCProduceproblem.insert", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: update</p>
	 * <p>Description: 修改方法</p>
	 * <p>return value: ServiceZMCC11</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:47:21 郭淑芳
	 * </p>
	 *
	 */
	@Override
	public EiInfo update(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			String problemTimeF = _dom.getFdProblemTimeF();
			String deadlineTimeF = _dom.getFdDeadlineTimeF();

			_dom.setFdProblemDate(this.getDay(problemTimeF)); // 发生日期
			_dom.setFdProblemTime(this.getTime(problemTimeF)); // 发生时间
			_dom.setFdDeadlineDate(this.getDay(deadlineTimeF)); // 解决日期
			_dom.setFdDeadlineTime(this.getTime(deadlineTimeF)); // 解决时间

			this.dao.update("ZMCCProduceproblem.update", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	/**
	 * 
	 * <p>Function: invalid</p>
	 * <p>Description: 作废 方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:48:13 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo invalid(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCCProduceproblem.invalid");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	
	/**
	 * 
	 * <p>Function: close</p>
	 * <p>Description: 关闭 方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:49:55 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo close(EiInfo inInfo) {
		EiInfo _temp = super.update(inInfo, "ZMCCProduceproblem.close");
		EiInfo outInfo = this.query(inInfo);
		outInfo.setMsg(_temp.getMsg());
		outInfo.setDetailMsg(_temp.getDetailMsg());
		return outInfo;
	}
	/**
	 * 
	 * <p>Function: slove</p>
	 * <p>Description: 下达整改通知</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:46:00 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo slove(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblemHandle _dom = new ZMCCProduceproblemHandle();
		EiBlock eiBlock = inInfo.getBlock("solve_add");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));

			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_HANDLE"));
			this.dao.insert("ZMCCProduceproblemHandle.insert", _dom);

			// 改主表状态 未查收
			Map<String, String> params1 = new HashMap<String, String>();
			params1.put("fdKeyId", _dom.getFdRecordId());
			this.dao.update("ZMCCProduceproblem.modifyByNotReceive", params1);

			// 更新主表 是否罚款字段
			Map<String, String> params2 = new HashMap<String, String>();
			params2.put("fdKeyId", _dom.getFdRecordId());
			params2.put("fdIsPenalty", _dom.getFdIsPenalty());
			this.dao.update("ZMCCProduceproblem.updatePenalty", params2);

			this.dao.update("ZMCCProduceproblemHandle.updatePenalty", params1);

			inInfo.set("fdKeyId", _dom.getFdRecordId());
			inInfo.set("fdRecordId", _dom.getFdRecordId());
			inInfo.set("fdHandleId", _dom.getFdKeyId());

		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: penalty</p>
	 * <p>Description: 罚款操作（考核）</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日上午10:45:41 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo penalty(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblemPenalty _dom = new ZMCCProduceproblemPenalty();
		EiBlock eiBlock = inInfo.getBlock("penalty_add");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {

			_dom.fromMap(eiBlock.getRow(i));
			Map<String, String> params = new HashMap<String, String>();
			params.put("fdKeyId", _dom.getFdKeyId());
			_dom.setFdKeyId(TablePk.getInstance().getNextValue("T_CC_PRODUCEPROBLEM_PENALTY"));
			_dom.setFdDepartment(Department.getInstance().getDepartmentNameById(_dom.getFdDepartmentId()));
			this.dao.insert("ZMCCProduceproblemPenalty.insert", _dom);

			// 改主表状态 已罚款，并且追加罚款主键ＩＤ，罚款总额
			Map<String, String> params1 = new HashMap<String, String>();
			params1.put("fdKeyId", _dom.getFdRecordId());
			params1.put("fdPenaltyIds", _dom.getFdKeyId());
			this.dao.update("ZMCCProduceproblem.modifyPenalty", params1);
			// 更新处理表中的数据
			if (_dom.getFdHandleId() != null && !_dom.getFdHandleId().trim().equals("")) {
				params1 = new HashMap<String, String>();
				params1.put("fdKeyId", _dom.getFdHandleId());
				params1.put("fdIsPenalty", "是");
				params1.put("fdPenaltyId", _dom.getFdKeyId());
				params1.put("fdPenalty", _dom.getFdPenalty().toString());
				this.dao.update("ZMCCProduceproblemHandle.updatePenalty", params1);
			} else {

				// 直接罚款，不处理时，改变主表状态为“关闭”
				Map<String, String> params2 = new HashMap<String, String>();
				params2.put("fdKeyId", _dom.getFdRecordId());

				this.dao.update("ZMCCProduceproblem.close", params2);
			}
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}

	/**
	 * 
	 * <p>Function: delay</p>
	 * <p>Description: 延期 方法</p>
	 * <p>return value:EiInfo</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月28日下午12:38:52 郭淑芳
	 * </p>
	 *
	 */
	public EiInfo delay(EiInfo inInfo) {
		EiInfo out = new EiInfo();
		ZMCCProduceproblem _dom = new ZMCCProduceproblem();
		EiBlock eiBlock = inInfo.getBlock("result");
		eiBlock.setBlockMeta(_dom.eiMetadata);
		int rowcount = eiBlock.getRowCount();
		int i = 0;
		for (; i < rowcount; i++) {
			_dom.fromMap(eiBlock.getRow(i));
			String deadlineTimeF = _dom.getFdDeadlineTimeF();
			_dom.setFdDeadlineDate(this.getDay(deadlineTimeF)); // 解决日期
			_dom.setFdDeadlineTime(this.getTime(deadlineTimeF)); // 解决时间

			this.dao.update("ZMCCProduceproblem.delay", _dom);
		}
		out = this.query(inInfo);
		out.setStatus(1);
		out.setMsg("操作" + rowcount + "记录成功");
		return out;
	}
	/**
	 * 
	 * <p>Function: getDay</p>
	 * <p>Description: 依据传入的字符串（yyyy-MM-dd hh:mm:ss）获取日期（yyyy-MM-dd）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午2:14:57 郭淑芳
	 * </p>
	 *
	 */
	private String getDay(String timeStr) {
		String[] tt = timeStr.split(" ");
		return tt[0];
	}
	
	/**
	 * 
	 * <p>Function: getTime</p>
	 * <p>Description: 依据传入的字符串（yyyy-MM-dd hh:mm:ss）获取时间（hh:mm:ss）</p>
	 * <p>return value:String</p>
	 * <p>throws Exception:</p>
	 * <p>History:
	 *    2016年9月27日下午2:18:19 郭淑芳
	 * </p>
	 *
	 */
	private String getTime(String timeStr) {
		String[] tt = timeStr.split(" ");
		return tt[1];
	}
}
